package com.jy.api.service.impl;

import com.jy.api.dao.ProductDao;
import com.jy.api.dao.SetMealDao;
import com.jy.api.dao.StoreIpAddressDao;
import com.jy.api.domain.Product;
import com.jy.api.domain.SetMeal;
import com.jy.api.domain.StoreIpAddress;
import com.jy.api.service.SetMealService;
import com.jy.api.service.StoreIpAddressService;
import com.jy.api.signUpReq.admin.SetMealReq;
import com.jy.api.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by xiaohua on 2019/3/1.
 */
@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealDao mealDao;
    @Autowired
    private ProductDao dao;
    @Autowired
    private StoreIpAddressDao addressDao;


    @Override
    @Transactional
    public Result addSetMeal(SetMealReq req) {
        List<SetMeal> meals = mealDao.findByStoreIdAndTypeIdAndEnabled(req.getStoreId(), req.getTypeId(), true);
        for (SetMeal m : meals) {
            if (StringUtils.equals(req.getName(), m.getName())) {
                return new Result(400, "该分类下的套餐名字已存在，请核实后进行添加");
            }
        }
        SetMeal meal = new SetMeal(req);
        if (req.isKitchenPrintEnabled()){
            StoreIpAddress address =addressDao.findOne(req.getIpId());
            meal.setIpAddress(address.getIpAddress());
            meal.setPort(address.getPort());
            meal.setIpId(address.getId());
        }
        mealDao.save(meal);
        return new Result(200, "套餐已添加成功");
    }

    @Override
    @Transactional
    public Result updateSetMeal(SetMealReq req) {
        List<SetMeal> meals = mealDao.findByStoreIdAndTypeIdAndEnabled(req.getStoreId(), req.getTypeId(), true);
        for (SetMeal m : meals) {
            if (StringUtils.equals(req.getName(), m.getName())) {
                return new Result(400, "该分类下的套餐名字已存在，请核实后进行添加!");
            }
        }
        SetMeal meal = mealDao.findOne(req.getId());
        meal.setName(req.getName());
        meal.setPrice(req.getPrice());
        meal.setTypeId(req.getTypeId());
        if (req.isKitchenPrintEnabled()){
            StoreIpAddress address =addressDao.findOne(req.getIpId());
            meal.setIpAddress(address.getIpAddress());
            meal.setPort(address.getPort());
            meal.setIpId(address.getId());
        }
        meal.setKitchenPrintEnabled(req.isKitchenPrintEnabled());
        mealDao.save(meal);
        return new Result(200, "套餐修改成功!");
    }

    @Override
    @Transactional
    public Result deleteSetMeal(SetMealReq req) {
        SetMeal meal = mealDao.findOne(req.getId());
        meal.setEnabled(false);
        List<Product> list = dao.findBySetMealIdAndEnabled(meal.getId(), true);
        List<Product> products = new ArrayList<>();
        for (Product p : list) {
            p.setSetMealId(0);
            p.setSetMealQty(0);
            products.add(p);
        }
        dao.save(products);
        mealDao.save(meal);
        return new Result(200, "套餐删除成功！");
    }

    @Override
    public List<SetMeal> findSetMealByStoreId(String storeId) {
        List<SetMeal> list = mealDao.findByStoreIdAndEnabled(storeId, true);
        return list;
    }

    @Override
    @Transactional
    public Result addProductByMealId(SetMealReq req) {
        SetMeal setMeal = mealDao.findOne(req.getId());
        if (null == setMeal) {
            return new Result(400, "该套餐不存在，请核实后进行添加套餐商品");
        }
        List<Product> list = new ArrayList<>();
        for (SetMealReq.ProIds r : req.getProIds()) {
            Product product = dao.findOne(r.getProductId());
            product.setSetMealId(req.getId());
            product.setSetMealQty(r.getSetMealQty());
            list.add(product);
        }
        dao.save(list);
        return new Result(200, "套餐商品添加成功!");
    }

    @Override
    public Result deleteProductByMealId(SetMealReq req) {
        List<Product> list = new ArrayList<>();
        for (SetMealReq.ProIds r : req.getProIds()) {
            Product product = dao.findOne(r.getProductId());
            product.setSetMealId(0);
            product.setSetMealQty(0);
            list.add(product);
        }
        dao.save(list);
        return new Result(200, "套餐商品删除成功!");
    }

    @Override
    public List<Product> findProductByMealId(int mealId) {
        return dao.findBySetMealIdAndEnabled(mealId, true);
    }
}
